package list;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author Summerday
 */
public class ListTest {

    List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3", "4"));

    /**
     * Java8 为Iterator新增了 forEachRemaining(Consumer action) 方法
     */
    public static void main(String[] args) {
        new ListTest().testSort();
    }

    public void testStream() {
        IntStream stream = IntStream.builder().add(1).add(2).add(3).build();
        int max = stream.max().getAsInt();
        System.out.println(max);
    }

    public void testForEach() {
        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        list.forEach(System.out::println);
    }

    public void testForEachRemaining() {
        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        Iterator<Integer> iterator = list.iterator();
        iterator.forEachRemaining(System.out::println);
    }

    public void testRemoveIf() {
        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        list.removeIf(x -> x % 2 == 0);
        list.forEach(System.out::println);
    }


    public void testRemove() {
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if ("1".equals(next)) {
                list.remove(next);
            }
        }
        System.out.println(list);
    }

    public void testCorrectRemove() {
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if ("1".equals(next)) {
                iterator.remove();
            }
        }
        System.out.println(list);
    }

    public void testRemoveNumber() {
        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        list.remove(new Integer(3));
        System.out.println(list);
    }

    public void testReplaceAll() {
        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        list.replaceAll(x -> x + 3);
        list.forEach(System.out::println);
    }

    public void testSort() {
        List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        list.sort((x, y) -> y - x);
        System.out.println(list);
    }
}
